// #ifndef CONTROL_H
// #define CONTROL_H

#pragma once

#include <mutex>
#include <thread>
#include <atomic>

/*
单例类，用于设置和获取日志管控条件
*/

#define MAX_LOG_FREQUENCY 5

typedef struct controlParameter{
  uint8_t log_level;           //日志输出等级
  uint8_t flush_level;         //日志刷新等级
  uint8_t log_flow;            //单挑日志最大流量kb
  uint8_t log_frequency;       //日志输出频率hz
  controlParameter(){
    log_level = 3;
    flush_level = 3;
    log_flow = 2;
    log_frequency = 0;
  }
}controlParameter_t;

class control
{
public:                                                                  
  static control *Instance(bool create_if_needed = true) {              
    static control *instance = nullptr;                                 
    if (!instance && create_if_needed) {                                  
      static std::once_flag flag;                                         
      std::call_once(flag,                                                
                     [&] { instance = new (std::nothrow) control(); }); 
    }                                                                     
    return instance;                                                      
  }  
                                                                       
public:
  void init(bool enable,controlParameter & param);
  bool get_enable_control(){return enable_control.load();};
  uint8_t get_log_level(){return parameter.log_level;};
  uint8_t get_flush_level(){return parameter.flush_level;};
  uint8_t get_log_flow(){return parameter.log_flow;};
  uint8_t get_log_frequency(){return parameter.log_frequency;};
  bool reset_log_frequency();
  void log_total_add(int temp_size);
  int get_log_total(){return log_total.load(std::memory_order_relaxed);}

private:
  void frequency();
private:
  std::thread thread_control;
  std::atomic<bool>  enable_control;   //是否启用管控
  std::atomic<bool>  init_control;     //管控类初始化
  std::atomic<int>   log_total;        //日志总量
  bool arrival_frequency;             //是否到达输出频率
  bool thread_join;
  controlParameter parameter;         //管控参数
private:
    control();
    ~control();
};

#ifdef SPDLOG_HEADER_ONLY
#include "control-inl.h"
#endif

// #endif